package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.bean.CertificationOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.OldCertificationContract;
import com.sanpu.kxe.business.main.presenter.OldCertificationPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.custom.SerializableHashMap;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.module.dialog.HintDialog;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.SoftInputUtil;

import butterknife.Bind;

/**
 * Description：认证人工审核第一步
 * Created by 薛勇军 on 2019/5/15.
 */

public class OldCertificateOneActivity extends BaseTitleActivity implements OldCertificationContract.View {

    @Bind(R.id.topLl)
    LinearLayout topLl;
    @Bind(R.id.name)
    EditTextWithDel vName;
    @Bind(R.id.identity_number)
    EditTextWithDel vIdentityNumber;
    @Bind(R.id.bank_card_number)
    EditTextWithDel vBankcardNumber;
    @Bind(R.id.bank_card_hint)
    ImageView vBankcardHint;
    @Bind(R.id.bank_phone_number)
    EditTextWithDel vBankphoneNumber;
    @Bind(R.id.bank_phone_hint)
    ImageView vBankphoneHint;
    @Bind(R.id.next)
    RoundTextView vNext;

    private OldCertificationContract.Presenter mPresenter;

    private CertificationOldOutput mOutput;

    private SerializableHashMap serializableHashMap;

    private boolean isComeFromNews = false;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_certification_old;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("认证");
        mPresenter = new OldCertificationPresenter(this);
        mPresenter.initDataView();
    }

    @Override
    protected void setListener() {
        super.setListener();
        vBankcardHint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = new TextView(mContext);
                textView.setText("为保障账户资金安全，只能绑定认证用户本人的银行卡。");
                new HintDialog(mContext)
                        .title("结算卡号说明")
                        .content(textView)
                        .show();
            }
        });
        vBankphoneHint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = new TextView(mContext);
                textView.setText("银行预留手机号码是办理该银行卡所填写的手机号码。预留手机号忘记或已停用，请联系银行客服更新处理。");
                new HintDialog(mContext)
                        .title("手机号说明")
                        .content(textView)
                        .show();
            }
        });
        vBankcardNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String current = s.toString().replace(" ", "");
                String format = CommonUtils.bankNumFormatter(current);
                vBankcardNumber.removeTextChangedListener(this);
                vBankcardNumber.setText(format);
                vBankcardNumber.addTextChangedListener(this);
            }

            @Override
            public void afterTextChanged(Editable s) {
                CharSequence text = vBankcardNumber.getText();
                if (text != null) {
                    Spannable spanText = (Spannable) text;
                    Selection.setSelection(spanText, text.length());
                }
            }
        });
        vNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isRespondEvent()) return;
                mPresenter.onCommitData(vName.getText().toString(), vIdentityNumber.getText().toString(),
                        vBankcardNumber.getText().toString(), vBankphoneNumber.getText().toString());
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        isComeFromNews = getIntent().getBooleanExtra(MainConfig.INTENT_COME_FROM_NEWS, false);
        if (isComeFromNews) {
            Bundle bundle = getIntent().getExtras();
            if (bundle.containsKey(MainConfig.INTENT_CERTIFICATE_FILE)) {
                serializableHashMap = (SerializableHashMap) bundle.get(MainConfig.INTENT_CERTIFICATE_FILE);
                CertificationOutput certificationOutput =
                        (CertificationOutput) getIntent().getSerializableExtra(MainConfig.INTENT_CERTIFICATION_INPUT);
                if (certificationOutput != null) {
                    if (!TextUtils.isEmpty(certificationOutput.getIdCardName())) {
                        vName.setText(certificationOutput.getIdCardName());
                    }
                    if (!TextUtils.isEmpty(certificationOutput.getIdCardNo())) {
                        vIdentityNumber.setText(certificationOutput.getIdCardNo());
                    }
                    if (!TextUtils.isEmpty(certificationOutput.getCardNo())) {
                        vBankcardNumber.setText(certificationOutput.getCardNo());
                    }
                }
            }
        }
    }

    @Override
    public void showViewData(CertificationOldOutput output) {
        if (output == null) return;
        this.mOutput = output;
        if (!TextUtils.isEmpty(output.getCrpNm())) {
            vName.setText(output.getCrpNm());
        }
        if (!TextUtils.isEmpty(output.getCrpIdNo())) {
            vIdentityNumber.setText(output.getCrpIdNo());
        }
        if (!TextUtils.isEmpty(output.getBankNo())) {
            vBankcardNumber.setText(output.getBankNo());
        }
        if (!TextUtils.isEmpty(output.getRsdTel())) {
            vBankphoneNumber.setText(output.getRsdTel());
        }
    }

    @Override
    public void showFirstDialog() {
        TextView mTextView = new TextView(mContext);
        mTextView.setText("近期发现犯罪分子通过资金流水、大额贷款、办理大额信用卡、承诺高收益等借口骗取用户注册，" +
                "获取用户账号与密码进行盗刷或诈骗等犯罪行为。本人承诺：已明确法律风险，绝不透露账号或密码供他人使用，" +
                "否则，由此产生的后果，本人需承担相应的法律责任，平台在此希望各位用户一起打造良好的支付环境！");
        new HintDialog(mContext).title("声明").content(mTextView).show();
    }

    @Override
    public void openCertificateSecondActivity() {
        if (mOutput == null) {
            mOutput = new CertificationOldOutput();
            mOutput.setChkSts(CommonConfig.AUTHEN_NOTSUB);
        }
        mOutput.setCrpNm(vName.getText().toString());
        mOutput.setCrpIdNo(vIdentityNumber.getText().toString());
        mOutput.setBankNo(vBankcardNumber.getText().toString().replace(" ", ""));
        mOutput.setRsdTel(vBankphoneNumber.getText().toString());
        MainOpenProvider.getInstance()
                .openOldCertificateSecondActivity(mContext, mOutput, serializableHashMap, isComeFromNews);
    }

    @Override
    protected void onPause() {
        super.onPause();
        SoftInputUtil.hideSoftInput(this);
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .statusBarView(R.id.topView)
                .statusBarDarkFont(true, 0.5f)
                .statusBarColor(R.color.white)
                .fullScreen(false)
//                .keyboardEnable(true)
                .init();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(OldCertificationContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
